package com.wteam.dragon.model.controller;

import com.wteam.dragon.model.pojo.bean.Dept;
import com.wteam.dragon.model.pojo.vo.DeptVo;
import com.wteam.dragon.model.service.DeptService;
import com.wteam.dragon.model.service.RolesDeptsService;
import com.wteam.dragon.system.aop.log.log.Log;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @Author 陈晓辉
 * @Date 2020/12/18 10:55
 */
@RestController
@Api(tags = "系统: 部门管理")
@RequestMapping("admin/dept")
@RequiredArgsConstructor
public class DeptController {
    private final DeptService deptService;
    private final RolesDeptsService rolesDeptsService;

    @Log("添加部门")
    @PostMapping("addDept")
    @ApiOperation("添加部门")
    @PreAuthorize("@el.check('addDept')")
    public ResultMessage addDept(DeptVo deptVo){
        Dept dept = BeanUtil.from(deptVo, Dept.class);
        return deptService.addDept(dept);
    }

    @Log("删除部门")
    @ApiOperation("删除部门")
    @DeleteMapping("delDept")
    @PreAuthorize("@el.check('delDept')")
    public ResultMessage delDept(Long deptId){
        return deptService.delDept(deptId);
    }

    @Log("更新部门")
    @ApiOperation("更新部门")
    @PutMapping("editDept")
    @PreAuthorize("@el.check('editDept')")
    public ResultMessage editDept(DeptVo deptVo){
        Dept dept = BeanUtil.from(deptVo, Dept.class);
        return deptService.editDept(dept);
    }

    @Log("获得部门列表树")
    @ApiOperation("获得部门列表树")
    @GetMapping("treeDept")
    @PreAuthorize("@el.check('treeDept')")
    public ResultMessage treeDept(String deptName){
        return deptService.treeDept(deptName);
    }

    @Log("获得部门列表名称")
    @ApiOperation("获得部门列表名称")
    @GetMapping("listDeptName")
    @PreAuthorize("@el.check('listDeptName')")
    public ResultMessage listDeptName(){
        return deptService.listDeptName();
    }

    @Log("将部门分配给角色")
    @ApiOperation("将部门分配给角色")
    @GetMapping("editRolesDepts")
    @PreAuthorize("@el.check('editRolesDepts')")
    public ResultMessage editRolesDepts(Long roleId, Set<Long> deptIds){
        return rolesDeptsService.editRolesDepts(roleId, deptIds);
    }

}
